package j40;

import cn.hutool.core.thread.ThreadUtil;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

/**
 * @author chengshuai
 * @date 2021/4/15 12:32
 */
public class SynchronousQueueDemo {
    public static void main(String[] args) {
        SynchronousQueue<Object> blockingQueue = new SynchronousQueue<>();
//        BlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(1);
        //生产者线程
        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "\t put 1");
                blockingQueue.put("1");
                System.out.println(Thread.currentThread().getName() + "\t put 2");
                blockingQueue.put("2");
                System.out.println(Thread.currentThread().getName() + "\t put 3");
                blockingQueue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"AAA").start();

        new Thread(() -> {
            try {
                ThreadUtil.sleep(5000);
                System.out.println(Thread.currentThread().getName() + "\t" + blockingQueue.take());
                ThreadUtil.sleep(5000);
                System.out.println(Thread.currentThread().getName() + "\t" + blockingQueue.take());
                ThreadUtil.sleep(5000);
                System.out.println(Thread.currentThread().getName() + "\t" + blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"BBB").start();
    }
}
